package linuxbox.news.servlet;

import javax.servlet.http.*;
import java.util.*;
import java.sql.*;

import linuxbox.common.Action;
import linuxbox.common.BoxSession;

import linuxbox.news.logic.BoxNews2;
import linuxbox.news.logic.BoxNewsEntry;

public class BoxNewsAction extends Action {
    BoxNews2 home = BoxNews2.getInstance();

    protected static String adminJSP = null;
    protected static String editJSP = null;

    public void doAction(HttpSession session, BoxSession aBoxSession,
            HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String str = (String) req.getParameter("VIEW_TYPE");
        if (str != null && str.length() > 0)
            session.setAttribute("news.VIEW_TYPE", str);

        str = (String) req.getParameter("SORTBY");
        if (str != null && str.length() > 0)
            session.setAttribute("news.SORTBY", str);

        str = (String) req.getParameter("ASC");
        if (str != null && str.length() > 0)
            session.setAttribute("news.ASC", str);

        str = req.getParameter("SubmitCancel");
        if (str != null) {
            sendFile(adminJSP, req, resp);
            return;
        }

        int curEntryID = 0;
        str = req.getParameter("ID");
        if (str != null) {
            curEntryID = Integer.parseInt(str);
        }
        str = (String) req.getParameter("SubmitSave");
        if (str != null) {
            saveToBean(curEntryID, req);
            sendFile(adminJSP, req, resp);
            return;
        }
        str = req.getParameter("SubmitUpdate");
        if (str != null) {
            saveToBean(curEntryID, req);
            sendFile(adminJSP, req, resp);
            return;
        }
        str = req.getParameter("SubmitDelete");
        if (str != null) {
            deleteBean(curEntryID);
            sendFile(adminJSP, req, resp);
            return;
        }

        str = req.getParameter("SubmitEdit");
        if (str != null) {
            // Now we'll go to the edit form
            if (curEntryID == 0) {
                setDefaultAttributes(req);
            } else {
                setBeanAttributes(req, curEntryID);
            }
            sendFile(editJSP, req, resp);
            return;
        }
        sendFile(adminJSP, req, resp);
    }

    protected void saveToBean(int id, HttpServletRequest req) {
        BoxNewsEntry entry = null;
        
        if (0 == id) {
            entry = new BoxNewsEntry();
        } else {
            entry = home.get(id);
        }

        String str = req.getParameter("SUBJECT");
        if (str != null)
            entry.setSubject(str);

        str = req.getParameter("CONTENT");
        if (str != null)
            entry.setContent(str);

        Calendar tmpCal = Calendar.getInstance();
        int tmpYear = tmpCal.get(Calendar.YEAR);
        int tmpMonth = tmpCal.get(Calendar.MONTH);
        int tmpDay = tmpCal.get(Calendar.DAY_OF_MONTH);

        str = req.getParameter("SUB_YEAR");
        if (str != null)
            tmpYear = Integer.parseInt(str);
        str = req.getParameter("SUB_MONTH");
        if (str != null)
            tmpMonth = Integer.parseInt(str);
        str = req.getParameter("SUB_DAY");
        if (str != null)
            tmpDay = Integer.parseInt(str);

        tmpCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
        entry.setSubTime(new Timestamp(tmpCal.getTime().getTime()));

        tmpYear = -1;
        tmpMonth = -1;
        tmpDay = -1;
        str = req.getParameter("EXP_YEAR");
        if (str != null)
            tmpYear = Integer.parseInt(str);
        str = req.getParameter("EXP_MONTH");
        if (str != null)
            tmpMonth = Integer.parseInt(str);
        str = req.getParameter("EXP_DAY");
        if (str != null)
            tmpDay = Integer.parseInt(str);

        if (tmpYear == -1 || tmpMonth == -1 || tmpDay == -1)
            entry.setExpTime(null);
        else {
            tmpCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
            entry.setExpTime(new Timestamp(tmpCal.getTime().getTime()));
        }

        if (id == 0) {
            home.add(entry);
        } else {
            entry.setId(id);
            home.update(entry);
        }
    }

    protected void deleteBean(int id) {
        if (id != 0) {
            home.remove(id);
        }
    }

    protected void setBeanAttributes(HttpServletRequest req, int ID) {
        BoxNewsEntry entry = home.get(ID);

        // Set the default attributes for a new entry
        req.setAttribute("ID", new Integer(entry.getId()).toString());
        req.setAttribute("SUBJECT", entry.getSubject());
        req.setAttribute("CONTENT", entry.getContent());

        Calendar tmpCal = Calendar.getInstance();
        Timestamp tmpTime = entry.getSubTime();
        if (tmpTime != null) {
            tmpCal.setTime(tmpTime);
            req.setAttribute("SUB_YEAR", new Integer(tmpCal.get(Calendar.YEAR))
                    .toString());
            req.setAttribute("SUB_MONTH", new Integer(tmpCal
                    .get(Calendar.MONTH)).toString());
            req.setAttribute("SUB_DAY", new Integer(tmpCal.get(Calendar.DATE))
                    .toString());
        } else {
            req.setAttribute("SUB_YEAR", "-1");
            req.setAttribute("SUB_MONTH", "-1");
            req.setAttribute("SUB_DAY", "-1");
        }
        tmpTime = entry.getExpTime();
        if (tmpTime != null) {
            tmpCal.setTime(tmpTime);
            req.setAttribute("EXP_YEAR", new Integer(tmpCal.get(Calendar.YEAR))
                    .toString());
            req.setAttribute("EXP_MONTH", new Integer(tmpCal
                    .get(Calendar.MONTH)).toString());
            req.setAttribute("EXP_DAY", new Integer(tmpCal.get(Calendar.DATE))
                    .toString());
        } else {
            req.setAttribute("EXP_YEAR", "-1");
            req.setAttribute("EXP_MONTH", "-1");
            req.setAttribute("EXP_DAY", "-1");
        }
    }

    protected void setDefaultAttributes(HttpServletRequest req) {
        // Set the defalut attributes for a new entry
        req.setAttribute("ID", "0");
        req.setAttribute("SUBJECT", "");
        req.setAttribute("CONTENT", "");

        Calendar today = Calendar.getInstance();
        int tmpYear = today.get(Calendar.YEAR);
        int tmpMonth = today.get(Calendar.MONTH);
        int tmpDay = today.get(Calendar.DATE);
        req.setAttribute("SUB_YEAR", new Integer(tmpYear).toString());
        req.setAttribute("SUB_MONTH", new Integer(tmpMonth).toString());
        req.setAttribute("SUB_DAY", new Integer(tmpDay).toString());
        req.setAttribute("EXP_YEAR", "-1");
        req.setAttribute("EXP_MONTH", "-1");
        req.setAttribute("EXP_DAY", "-1");
    }

    public void setAdminJSP(String str) {
        adminJSP = str;
    }

    public void setEditJSP(String str) {
        editJSP = str;
    }
}
